package sg.edu.nus.iss.team2s.jass.control;

import sg.edu.nus.iss.team2s.jass.dao.*;
import sg.edu.nus.iss.team2s.jass.dto.*;
import sg.edu.nus.iss.team2s.jass.gui.JobAdvertResponseScreen;
import sg.edu.nus.iss.team2s.jass.gui.LoginScreen;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.Calendar;
import java.awt.Frame;


/**
 * <p>The <strong>JobAdvertController</strong> is to broker object between screen objects and job advert DAO. 
 * This class call the functionalities from JobAdvertDAO({@link sg.edu.nus.iss.team2s.jass.dao.JobAdvertDAO}),CompanyDAO({@link sg.edu.nus.iss.team2s.jass.dao.CompanyDAO}) ,AdvertiserDAO({@link sg.edu.nus.iss.team2s.jass.dao.AdvertiserDAO}).<p>
 * 
* @author Zheng xiaofeng,Aye Thazin Kyaw,Lu Zhixian,Nie Xiaoyi
* @version 1.1
* 
*/
public class JobAdvertController {
	
	/**
	 * This method is called by JobAdvertDetailScreen to create new jobadvert object and save into property file
	 * 
	* @author Nie Xiao Yi
    * @param JobAdvert object
	* @return boolean
	* @exception  Exception due to retrieve data error from the database.
	* @throw DAOException
	*/
	
	public static boolean addJobAdvertEntry(JobAdvert jobAdvert) throws DAOException{
		JobAdvertDAO userInterface = (JobAdvertDAO)DAOFactory.getInstance().getDAO(JobAdvert.class.getName());
		try {
			userInterface.add(jobAdvert);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
		return false;
	}
	
	/**
	 * This method is called by JobAdvertDetailScreen to display JobAdvertResponse Screen.
	 * 
	* @author Zheng xiaofeng
     * @param JobAdvert object, the caller frame object
	 * @return void
	 * @exception  Exception due to retrieve data error from the database.
	*/
		
	public static void apply(JobAdvert jobAdvert, Frame parent){
		if(LoginController.checkLoginStatus()){
			User user = MainController.getUser();
			JobAdvertResponseScreen responseScreen = new JobAdvertResponseScreen(jobAdvert, user);
			responseScreen.setVisible(true);
		}
		else{
			LoginScreen loginScreen = new LoginScreen(parent);
			loginScreen.setVisible(true);
		}
	}
	
	/**
	 * This method is to get the list of company listed based jobadvertiserId
	 * 
	* @author Lu ZhiXian
     * @param advertiser's unique key jobAdvertId
	 * @return List of Company Object
	 * @exception  DAOException due to retrieve data error from the database.
	 * @throws Exception
	*/
	public static List<Object> getCompanyList(String jobAdvertiserId) throws Exception{
		try{
			CompanyDAO c=(CompanyDAO)DAOFactory.getInstance().getDAO(Company.class.getName());
		   return c.retrieveAllCompanies(jobAdvertiserId);
			
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
	
	}
	
	/**
	 * This is to get advertiser object by job advertiser's loginId
	 * 
	* @author Lu ZhiXian
     * @param user's loginId
	 * @return advertiser
	 * @exception  DAOException due to retrieve data error from the database.
	 * @throws Exception
	*/
	public static Advertiser getAdvertiser(String loginId)throws Exception{
		Advertiser obj;
		try{
			
			AdvertiserDAO jad=(AdvertiserDAO)DAOFactory.getInstance().getDAO(Advertiser.class.getName());
			
			obj=(Advertiser)jad.getAdvertiserByLogin(loginId);
			
			return obj;
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
			throw exp;
		}
	}
	
	/**
	 * This is to get company object by companyId
	* @author Lu ZhiXian
     * @param Company unique key companyId
	 * @return Company Object
	 * @exception  DAOException due to retrieve data error from the database.
	 * @throws Exception
	*/
	public static Company getCompany(String companyId)throws Exception{
		Company obj;
		try{
			
			CompanyDAO jad=(CompanyDAO)DAOFactory.getInstance().getDAO(Company.class.getName());
			
			obj=(Company)jad.get(companyId);
			
			return obj;
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
			throw exp;
		}
	}
	
	
	/**
	 * This method is called by main screen search part.
	 * 
	 * @author Aye Thazin Kyaw
     * @param  keyword , the keywork user key in the search text field, can be any words in job title/description
     *         salaryRange, the value user select from salaryRange dropdwon field
     *         postDate, the dateRange user select in the search options.
     *         
	 * @return ArrayList of the matched JobAdvert objects.
	 * @exception  DAOException due to retrieve data error from the database.
	 * @throws Exception
	*/
	public static ArrayList<JobAdvert> searchJobAdvert(String keyword, String salaryRange,String postDate) throws Exception{
		
		DAOInterface dao = (DAOInterface)DAOFactory.getInstance().getDAO(JobAdvert.class.getName());
		List<Object> list = null;
		ArrayList<JobAdvert> resultList = new ArrayList<JobAdvert>();
		String advertiserId="";
		
		try {
			
			if (LoginController.checkLoginStatus()){
				User user = MainController.getUser();	
				if (user.getUserType().equals(String.valueOf(User.JOBADVERTISER))){		
					Advertiser advertiser=JobAdvertController.getAdvertiser(user.getLoginName());
					
					advertiserId=advertiser.getAdvertiserId();	
				}
		
			}
			
			list = dao.list();
			Iterator<Object> iterator = list.iterator();
			while(iterator.hasNext())
			{
				boolean bName = true;
				boolean bSalary = true;
				boolean bPostDate = true;
				boolean bStatus = false;
				JobAdvert jobAdvert = (JobAdvert)iterator.next();
				if(!keyword.equals("") && (jobAdvert.getJobTitle().indexOf(keyword) < 0 && jobAdvert.getJobDesc().indexOf(keyword) < 0))
					bName = false;
				
				if(bName && !salaryRange.equals("All"))
				{
					if(!salaryRange.equals(jobAdvert.getSalaryRange()))
						bSalary = false;
				}
				
				if(bName && bSalary && !postDate.equals("All"))
				{
					Calendar calToday = Calendar.getInstance();
					calToday.set(calToday.get(Calendar.YEAR),
							calToday.get(Calendar.MONTH),
							calToday.get(Calendar.DAY_OF_MONTH),
							  23,   // Hour
							  59,   // Minute
							  59);  // Second
					
					
					Calendar calPostDate = Calendar.getInstance();
					calPostDate.setTime(jobAdvert.getPostDate());
					
					
					if(postDate.equals("within one day"))
					{
						calToday.add(Calendar.DATE, -1);
						if(calPostDate.before((calToday)))
							bSalary = false;
					}
					else if(postDate.equals("within one week"))
					{
						calToday.add(Calendar.DATE, -8);
						if(calPostDate.before((calToday)))
							bSalary = false;
					}
					else if(postDate.equals("within one month"))
					{
						calToday.add(Calendar.DATE, -31);
						if(calPostDate.before((calToday)))
							bSalary = false;
					}
				}
				
				//only show active job advertisement 
				//and withdrawed job advertisement done by current logined job advertiser
				if(jobAdvert.getStatus() == JobAdvert.ACTIVE)
				{
					bStatus = true;
				}
				else if (jobAdvert.getStatus() == JobAdvert.WITHDRAW)
				{
					if (advertiserId.equalsIgnoreCase(jobAdvert.getAdvertiserId())){
								bStatus = true;
					}
						
				}
				if(bName && bSalary && bPostDate && bStatus)
				{
					resultList.add(jobAdvert);
				}
				
			}
			
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return resultList;	
	}

	/**
	 * this is to get jobAdvert object by jobAdvertId
	 * 
	* @author Lu ZhiXian
     * @param JobAdvert unique key jobAdvertId
	 * @return JobAdvert object
	 * @exception  DAOException due to retrieve data error from the database.
	 * @throws Exception
	*/
	public static JobAdvert getJobAdvert(String jobAdvertId)throws Exception {
		JobAdvert obj;
		try{
			
			JobAdvertDAO jad=(JobAdvertDAO)DAOFactory.getInstance().getDAO(JobAdvert.class.getName());
			
			obj=(JobAdvert)jad.get(jobAdvertId);
			
			return obj;
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
			throw exp;
		}
	
	}
	
	/**
	 * this method is called by JobDetailScreen to update jobadvert into property file
	* @author Lu ZhiXian
     * @param JobAdvert object
	 * @return void
	 * @exception  DAOException due to retrieve data error from the database.
	 * @throws Exception
	*/
	
	public static void updateJobAdvert(JobAdvert obj)throws Exception {
		
		try{
			
			JobAdvertDAO jad=(JobAdvertDAO)DAOFactory.getInstance().getDAO(JobAdvert.class.getName());
			
			jad.updateJobAdvert(obj);
			
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
			throw exp;
			
		}
	
	}
	
	/**
	*  this method is called by JobDetailScreen to update jobadvert into property file
	* @author Lu ZhiXian
     * @param JobAdvert object
	 * @return void
	 * @exception  DAOException due to retrieve data error from the database.
	 * @throws Exception
	*/
	public static void withdrawJobAdvert(JobAdvert obj)throws Exception {
		
		try{
			
			JobAdvertDAO jad=(JobAdvertDAO)DAOFactory.getInstance().getDAO(JobAdvert.class.getName());
			
			jad.withdrawJobAdvert(obj);
			
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
			throw exp;
			
		}
	
	}
	
	/**
	* this method is called by JobDetailScreen to update jobadvert into property file
	* @author Lu ZhiXian
     * @param JobAdvert object
	 * @return void
	 * @exception  DAOException due to retrieve data error from the database.
	 * @throws Exception
	*/
	public static void deleteJobAdvert(JobAdvert obj)throws Exception {
		
		try{
			
			JobAdvertDAO jad=(JobAdvertDAO)DAOFactory.getInstance().getDAO(JobAdvert.class.getName());
			
			jad.deleteJobAdvert(obj);
			
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
			throw exp;
			
		}
	
	}
	
}
